/**
 * Created by root on 2017/8/1.
 * 返回主页面的最新笔记
 */
window.onload=function(userId){
    var mytitle=document.getElementsByTagName("article")[0].firstChild;
    var mydate=document.getElementById("date");
    var mytext=document.getElementsByTagName("article")[0].lastChild;
    var search_button=document.getElementsByClassName("search")[1];
    var search_text=document.getElementsByClassName("search")[0].value;
    var edit_button=document.getElementById("edit");
    var note_id=document.getElementById("note_id");
    var new_button=document.getElementsByClassName("left_button")[0];

	alert("userId2: "+userId);
	
//加载最新笔记
    var request=new XMLHttpRequest();
    request.open("POST","/i/note/note/getLast",true);
	
	//没写发送的数据
	var sendData="userId="+document.getElementById("username").value;
               
				
				
    request.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    request.onreadystatechange=function(){
        if(request.readyState==4)
        {
            if(request.status==200)
            {
                var data=JSON.parse(request.responseText);
                //检测
                if(data.result==1)
                {
                    alert("success");
                    mytitle.innerHTML=data.title;
                    mydate.innerHTML=data.updateTime;
                    mytext.innerHTMl=data.text;
                    note_id.innerHTML=data.noteId;
                }
            }
            else
            {
                alert("请求错误"+request.status);
            }
        }else{ 
		alert("readyState:"+request.readyState);
		}
    }
    request.send(sendData);

    //加载搜索的笔记
    search_button.onclick=function(){
        var xmlhttp=new XMLHttpRequest();
        xmlhttp.open("POST","/i/note/note/search",true);
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        var data="title="+search_text;
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4)
            {
                if(xmlhttp.status==200)
                {
                    var search_result=JSON.parse(xmlhttp.responseText);
                    if(search_result.result==1)
                    {
                        alert("search success");                 //调试
                        mytitle.innerHTML=search_result.title;
                        mydate.innerHTML=search_result.updateTime;
                        mytext.innerHTMl=search_result.text;
                        note_id.innerHTML=search_result.noteId;
                    }
                    else{
                       alert("无此条笔记的搜索结果！");
                    }
                }
            }
        }
        xmlhttp.send(data);
    }
    function setCookie(cname,cvalue)
    {

        document.cookie = cname + "=" + cvalue + "; ";
    }
    function getCookie(cname)
    {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i=0; i<ca.length; i++)
        {
            var c = ca[i].trim();
            if (c.indexOf(name)==0) return c.substring(name.length,c.length);
        }
        return "";
    }
//编辑按钮：设置cookie
    edit_button.onclick=function(){
        var y=note_id.innerHTML;
        //var y=mydate.innerHTML;
        setCookie("note",y);
        window.location.href="editLast.html";
    }
    //新建笔记按钮:删除cookie
    new_button.onclick=function(){
        document.cookie = "note=; expires=Thu, 01 Jan 1970 00:00:00 GMT";
        window.location.href="editLast.html";
    }
}



